package com.franklin.java.api.psi.data.resolver;

import com.franklin.plugins.common.collection.PrettyArrayList;
import com.franklin.java.api.psi.data.PsiData;
import com.intellij.psi.PsiElement;

import java.util.List;
import java.util.Objects;

/**
 * 解析器工厂
 * @author Franklin
 * @since 2021/08/15/ 21:13
 */
public class PsiDataResolverFactory implements PsiDataResolver {

    private final static PsiDataResolverFactory INSTANCE = new PsiDataResolverFactory();

    private final static List<PsiDataResolver> resolvers = new PrettyArrayList<>();

    static {
        resolvers.add(new BasicTypeResolver());
        resolvers.add(new NewObjectResolver());
        resolvers.add(new LambdaExpressionResolver());
        resolvers.add(new ForeachLoopResolver());
        resolvers.add(new MethodBodyResolver());
        resolvers.add(new MethodParameterResolver());
        resolvers.add(new ClassFieldResolver());
        resolvers.add(new MethodReferenceResolver());
    }

    public static PsiData resolveData(String name, String value, PsiElement dataElement){
        return INSTANCE.resolve(name, value, dataElement);
    }

    @Override
    public PsiData resolve(String name, String value, PsiElement dataElement) {
        for (PsiDataResolver resolver : resolvers) {
            PsiData psiData = resolver.resolve(name, value, dataElement);
            if (Objects.nonNull(psiData)){
                return psiData;
            }
        }
        return new PsiData("String",value,dataElement,"java.lang.String", dataElement);
    }
}
